/*
 * @copyright fxsen <br/>
 * @date 2022/4/25 0:24 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.platform.web.controllers;


import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResponseResult;
import com.fxsen.commons.sysmanager.core.entity.Dictionary;
import com.fxsen.commons.sysmanager.core.entity.DictionaryDetail;
import com.fxsen.commons.sysmanager.core.service.IDictionaryCategoryService;
import com.fxsen.commons.sysmanager.core.service.IDictionaryDetailService;
import com.fxsen.commons.sysmanager.core.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 字典控制层
 * @author fxsen
 * @date 2022/4/25 0:24
 * @email <a href="mailto:fxsen@foxmail.com">
 */
@RestController
@RequestMapping("/dictionary")
@RequiredPermission("dictionary")
@Api(tags = "系统：字典接口")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IDictionaryCategoryService dictionaryCategoryService;
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;
    @ApiOperation(value = "新增")
    @PostMapping("save")
    @RequiredPermission("dictionary:save")
    @RecordLog("新增字典")
    public ResponseResult save(@RequestBody Dictionary dictionary){
        try {
            if (null == dictionary || StringUtils.isBlank(dictionary.getCategoryId())){
                return ResponseResult.failed("未传请求参数！");
            }
            String name = dictionary.getName();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("字典名称不能为空！");
            }
            String code = dictionary.getCode();
            if (StringUtils.isBlank(code)){
                return ResponseResult.failed("字典编码不能为空！");
            }
            if (dictionaryService.isExist("name",name)){
                return ResponseResult.failed("字典名称”"+name+"“已存在！");
            }
            if (dictionaryService.isExist("code",code)){
                return ResponseResult.failed("字典编码”"+code+"“已存在！");
            }
            boolean save = dictionaryService.save(dictionary);
            return save ? ResponseResult.success() : ResponseResult.saveFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RequiredPermission("dictionary:update")
    @RecordLog("更新字典")
    public ResponseResult update(@RequestBody Dictionary dictionary){
        try {
            if (null == dictionary || StringUtils.isBlank(dictionary.getCategoryId()) || StringUtils.isBlank(dictionary.getId())){
                return ResponseResult.failed("未传请求参数！");
            }
            String name = dictionary.getName();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("字典名称不能为空！");
            }
            String code = dictionary.getCode();
            if (StringUtils.isBlank(code)){
                return ResponseResult.failed("字典编码不能为空！");
            }
            Dictionary oldDict = dictionaryService.getById(dictionary.getId());
            if (!oldDict.getName().equals(name) && dictionaryService.isExist("name",name)){
                return ResponseResult.failed("字典名称”"+name+"“已存在！");
            }
            if (!oldDict.getCode().equals(code) && dictionaryService.isExist("code",code)){
                return ResponseResult.failed("字典编码”"+code+"“已存在！");
            }
            BeanUtils.copyProperties(dictionary,oldDict);
            boolean update = dictionaryService.updateById(oldDict);
            return update ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RequiredPermission("dictionary:delete")
    @RecordLog("删除字典")
    public ResponseResult delete(@ApiParam("字典主键id") @RequestParam("id") String id){
        try {
            boolean remove = dictionaryService.removeById(id);
            if (remove){
                //删除字典详情
                List<DictionaryDetail> dictionaryDetailList = dictionaryDetailService.getByDictionaryId(id);
                if (CollectionUtils.isNotEmpty(dictionaryDetailList)){
                    dictionaryDetailService.removeBatchByIds(dictionaryDetailList);
                }
                return ResponseResult.success();
            }
            return ResponseResult.deleteFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "查询单个字典")
    @GetMapping("get/{id}")
    public ResponseResult get(@PathVariable String id){
        try {
            return ResponseResult.success(dictionaryService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "查询单个类别下的字典")
    @GetMapping("list")
    public ResponseResult list(@ApiParam("类别ID") @RequestParam("categoryId") String categoryId){
        try {
            return ResponseResult.success(dictionaryService.getByCategoryId(categoryId));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
}
